package org.west.sky.frame.activiti.study.chapter16;

import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.west.sky.frame.activiti.study.util.ActivitiEngineUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * author: chz
 * date: 2025/6/6
 * description: 事件网关
 */
public class RunDemo16_2_4 extends ActivitiEngineUtil {

    public static void main(String[] args) {
        RunDemo16_2_4 runDemo16_1_2 = new RunDemo16_2_4();
        runDemo16_1_2.run();
    }

    public void run() {

        ProcessDefinition processDefinition = deployByClassPathResource("processes/chapter16/Chapter16_2_4.bpmn20.xml");

        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId());

        Task task1 = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
        System.out.println("====当前流程：" + processInstance.getId() + "进行到：" + task1.getName() + "：任务节点");
        taskService.complete(task1.getId());

        Random random = new Random();
//        int i = random.nextInt(10);
        int i = 6;
        System.out.println("====当前随机数：" + i);
        if (i > 5) {

        } else {
            runtimeService.signalEventReceived("theSignal");
        }

        while (true) {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            Task task2 = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
            if (null != task2) {
                System.out.println("====当前流程：" + processInstance.getId() + "进行到：" + task2.getName() + "：任务节点");
                taskService.complete(task2.getId());
                break;
            } else {
                System.out.println("====当前流程：" + processInstance.getId() + "进行到‘等待’任务节点");
            }
        }


        closeEngine();
    }
}
